import fi from "element-ui/src/locale/lang/fi";

export const util = {
    methods:{
        // 用户信息相关
        setUserMsg(userInfo){
            sessionStorage.setItem("manageUserName",userInfo.nickName);
            sessionStorage.setItem("manageToken",userInfo.token);
            sessionStorage.setItem("manageUserInfo",JSON.stringify(userInfo));
        },
        removeUserInfo(){
            sessionStorage.removeItem("manageUserName");
            sessionStorage.removeItem("manageToken");
            sessionStorage.removeItem("manageUserInfo");
        },
        getUserInfo(){
            return JSON.parse(sessionStorage.getItem("manageUserInfo"));
        },
        //左边提示信息
        notify(title,type){
            this.$notify({
                title: title ? title : "操作成功!",
                type: type ? type : "success"
            })
        },
        message(title,type){
            this.$message({
                title : title ? title : "操作成功!",
                type : type ? type : "success"
            })
        },
        // 表格公共的成功提示并调用方法
        baseSuccessTip(title,type,that){
            this.$notify({
                title: title ? title : "操作成功!",
                type: type ? type : "success"
            })
            that.getTableData();
        },
        seeBigImg(url) {
            this.imgDiaUrl = url
            this.imgVisible = true;
        },
        getUploadUrl(id,moduleName){
            return `${this.$store.state.imgHOST}${moduleName}/upload/${id}`
        },
        //上传图片之前的校验
        beforeAvatorUpload(file){
            const isJPG = (file.type === 'image/jpeg')||(file.type === 'image/png');
            if(!isJPG){
                this.$message.error('上传头像图片只能是jpg或png格式');
                return false;
            }
            const isLt5M = (file.size / 1024 /1024) < 5;
            if(!isLt5M){
                this.$message.error('上传头像图片大小不能超过5MB');
                return false;
            }
            return true;
        },
        //上传图片成功之后要做的工作
        handleAvatorSuccess(res){
            let _this = this;
            if(res.code == 200){
                _this.getTableData();
                _this.$notify({
                    title: '上传成功',
                    type: 'success'
                });
            }else{
                _this.$notify({
                    title: '上传失败',
                    type: 'error'
                });
            }
        },
        //把已经选择的项赋值给multipleSelection
        handleSelectionChange(val){
            if(val && val.length > 0){
                this.multipleSelection.splice(0);
                for(let item of val){
                    this.multipleSelection.push(item.id)
                }
            }else {
                this.multipleSelection.splice(0);
            }
        },
        add() {
            this.diaTitle = "添加";
            this.openDia = true;
        },
        //  每页数量发生变化
        pageSizeChange(val) {
            this.params.pageSize = val;
            this.getTableData();
        },
        //  当前页发生变化
        currentChange(val) {
            this.params.current = val;
            this.getTableData();
        },
        // 关闭新增或修改对话框
        closeDia(obj){
            this.openDia = false;
            for(let key in obj){
                obj[key]  = undefined
            }
        },
        reset(obj){
            for(let key in obj){
                obj[key]  = undefined
            }
        },
        updateById(row,fromData) {
            this.diaTitle = "修改";
            this.openDia = true;
            Object.keys(fromData).forEach(key=>{fromData[key]=row[key]})
        },
        getToken(){
            return sessionStorage.getItem("manageToken");
        },
        getSchoolListAll(){
            this.$basePostRequest("/examSchool/getListAll",{})
                .then((res) =>{
                    this.schoolList = res;
                });
        },
        //得到所有课程信息
        getLessonListAll(param) {
            this.$basePostRequest("/examLesson/getListAll", param ? param : {})
                .then((res) => {
                    this.lessonList = res;
                });
        },
        resetObj() { // 重置数据
            let obj = this.params
            for (let key in obj) {
                if (key == 'current') {
                    obj[key] = 1
                }
                if (key == 'pageSize') {
                    obj[key] = 5
                } else {
                    obj[key] = undefined
                }
            }
            this.params = obj
            this.getTableData();
        },

    }
}
